package com.example.controller;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections4.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.example.entity.Netfollows;
import com.example.entity.NetfollowsVO;
import com.example.entity.Students;
import com.example.entity.StudentsVO;
import com.example.service.NetfollowsService;
import com.example.service.StudentManageService;
import com.example.service.UserService;
import com.example.util.DownloadUtil;
import com.example.util.ExcelUtil;
import com.example.util.FileUtil;


@RestController
@RequestMapping("DAO")
public class DAOController {
	
	@Autowired
	private  NetfollowsService netfollowsService;
	@Autowired
	private UserService userService;
	@Autowired
	private StudentManageService studentManageService;
	
	/**
	 * 导出Excel数据  跟踪管理
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/export" , method = RequestMethod.GET, produces = "application/json")
	public void export(HttpServletResponse response) throws Exception{
		ByteArrayOutputStream baos =null;
		OutputStream out =null;
		try {
			
			NetfollowsVO netfollowsVO = new NetfollowsVO();
			List<Netfollows> dataList = netfollowsService.GetnetfollowsLayUIzixunjingli(netfollowsVO);
			
			baos = ExcelUtil.exportExcel(dataList, Netfollows.class, "跟踪管理.xlsx", 2);
			DownloadUtil.generalDownload(response, baos, "跟踪管理.xlsx");
		} catch (Exception e) {
			e.printStackTrace();
		    throw new Exception("导出失败：" + e.getMessage());
		} finally {
			if(baos != null){
				baos.close();
			}
			if(out != null){
				out.close();
			}
		}
	}
	
	
	/**
	 * 导出Excel数据  网络学生管理
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/Wlexport" , method = RequestMethod.GET, produces = "application/json")
	public void exportWl(HttpServletResponse response) throws Exception{
		ByteArrayOutputStream baos =null;
		OutputStream out =null;
		try {
			
			StudentsVO studentsVO= new StudentsVO();
		    List<Students> dataList =  userService.findStudents(studentsVO);
		    
			baos = ExcelUtil.exportExcel(dataList, Students.class, "网络学生管理.xlsx", 2);
			DownloadUtil.generalDownload(response, baos, "网络学生管理.xlsx");
		} catch (Exception e) {
			e.printStackTrace();
		    throw new Exception("导出失败：" + e.getMessage());
		} finally {
			if(baos != null){
				baos.close();
			}
			if(out != null){
				out.close();
			}
		}
	}
	
	
	/**
	 * 导入Excel数据 网络学生管理
	 * @param file
	 */
	@RequestMapping(value = "/import", method = RequestMethod.POST)
	//@RequestMapping(value = "/import",method = RequestMethod.GET, produces = "application/json")
	public Map<String, Object> importExcel(MultipartFile file, String netpusherid,String createUser) {
		File f = null;
		try {
			// MultipartFile 转  file
			f = FileUtil.multipartFileToFile(file);
			List<Students> list = ExcelUtil.importExcel(f, Students.class, 2);
			for (Students data : list) {
				data.setNetpusherid(netpusherid);
				data.setCreateUser(createUser);
				studentManageService.insertstudent(data);
			}
			Map<String, Object> map = new HashedMap<>();
			map.put("code",0);
			map.put("msg","导入成功");
			return map;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (f != null) {
				f.delete();
			}
		}
		Map<String, Object> map = new HashedMap<>();
		map.put("code",0);
		map.put("msg","导入成功");
		return map;
		
	}

}
